package fractal.view;

import fractal.app.AppConfig;
import fractal.app.AppDefine;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;

/** Class make menu application */
public class AppMenu extends JMenuBar {

	private MenuGroup file;
	private MenuGroup window;
	private MenuGroup options;
	private MenuGroup optionsSkin;
	private MenuGroup help;
	private MenuItemCheck windowStartPage;
	private MenuItemCheck windowFractal1;
	private MenuItemCheck windowFractal2;
	private MenuItemRadio optionsSkinSwing;
	private MenuItemRadio optionsSkinSystem;

	/** constructor argument: application view */
	public AppMenu(AppView appView) {

		//flags:
		boolean hasSwing = AppConfig.getInstance().get(AppDefine.APPSKIN).equals(AppDefine.SKINDEF);

		//keys:
		final KeyStroke ctrlN = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK);
		final KeyStroke ctrlR = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK);
		final KeyStroke ctrlQ = KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK);

		//menu:
		this.file = new MenuGroup("File");
		this.options = new MenuGroup("Options");
		this.optionsSkin = new MenuGroup("Skins");
		this.window = new MenuGroup("Window");
		this.help = new MenuGroup("Help");

		this.file.add(new MenuItem("New", AppDefine.STARTNEW, ctrlN, appView));
		this.file.separator();
		this.file.add(new MenuItem("Restart", AppDefine.RESTART, ctrlR));
		this.file.add(new MenuItem("Exit", AppDefine.EXIT, ctrlQ));

		this.optionsSkinSwing = new MenuItemRadio("defoult", AppDefine.SKINDEF, hasSwing, appView);
		this.optionsSkinSystem = new MenuItemRadio("system", AppDefine.SKINSYS, !hasSwing, appView);

		ButtonGroup gr1 = new ButtonGroup();
		gr1.add(this.optionsSkinSwing);
		gr1.add(this.optionsSkinSystem);

		this.optionsSkin.add(this.optionsSkinSwing);
		this.optionsSkin.add(this.optionsSkinSystem);
		this.options.add(this.optionsSkin);
		this.options.separator();
		this.options.add(new MenuItem("Defoult setup", AppDefine.DEFAULT, appView));

		this.windowStartPage = new MenuItemCheck("Start page", AppDefine.STARTNEW, true, appView);
		this.windowFractal1 = new MenuItemCheck(AppDefine.FRACTAL1, AppDefine.FRACTAL1, false, appView);
		this.windowFractal2 = new MenuItemCheck(AppDefine.FRACTAL2, AppDefine.FRACTAL2, false, appView);

		ButtonGroup gr2 = new ButtonGroup();
		gr2.add(this.windowStartPage);
		gr2.add(this.windowFractal1);
		gr2.add(this.windowFractal2);

		this.window.add(this.windowStartPage);
		this.window.separator();
		this.window.add(this.windowFractal1);
		this.window.add(this.windowFractal2);

		this.help.add(new MenuItem("Home page", AppDefine.HOMEPAGE, appView));
		this.help.add(new MenuItem("About...", AppDefine.ABOUT, appView));

		super.add(this.file);
		super.add(this.options);
		super.add(this.window);
		super.add(this.help);
	}

	/** method set skin application */
	public void setSkin(String name) {
		this.optionsSkinSwing.setSelected(name.equals(AppDefine.SKINDEF));
		this.optionsSkinSystem.setSelected(name.equals(AppDefine.SKINSYS));
	}

	/** method set view panel application */
	public void setWindow(String name) {
		this.windowStartPage.setSelected(name.equals(AppDefine.STARTNEW));
		this.windowFractal1.setSelected(name.equals(AppDefine.FRACTAL1));
		this.windowFractal2.setSelected(name.equals(AppDefine.FRACTAL2));
	}
}
